/*
  tempgraph: A utility for plotting historical climatology graphs
  Copyright (C) 2009-2013 Bob Mottram
  bob@sluggish.dyndns.org

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef TEMPGRAPH_H
#define TEMPGRAPH_H

#include <QMainWindow>
#include <QFileDialog>
#include <QImage>
#include <QMessageBox>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QTime>
#include <QDesktopServices>
#include <QUrl>
#include <stdio.h>
#include <ctime>
#include <iostream>
#include "globaldata.h"
#include <unistd.h>
#include <QLabel>
#include <QThread>

#ifndef _WIN32
    #define TEMPGRAPH_BINARY "/usr/bin/tempgraph"
    #define COPY_FILE "install -m 644"
    #define DELETE_FILE "rm -f"
    #define MAKE_DIR "mkdir"
    #define PATH_SEPARATOR "/"
#else
    #define TEMPGRAPH_BINARY "C:\tempgraph"
    #define COPY_FILE "copy"
    #define DELETE_FILE "del"
    #define MAKE_DIR "mkdir"
    #define PATH_SEPARATOR "\\"
#endif

using namespace std;

namespace Ui {
class TempGraph;
}

class TempGraph : public QMainWindow
{
    Q_OBJECT
    
public slots:
    int calculate();
    void openDataSources();
    int saveImageAs();
    void openImage();
    void showGraph();
    void openAbout();

public:
    GlobalData globals;
    int ref_year_start;
    int ref_year_end;
    int image_width, image_height;
    float subtitle_vertical_position;

    static bool fileExists(QString filename);

    explicit TempGraph(QWidget *parent = 0);
    ~TempGraph();
    
private:
    Ui::TempGraph *ui;    
    QImage image;
    QGraphicsPixmapItem * image_item;
    QGraphicsScene * image_scene;
    vector<QString> country;
    vector<QString> graphType;
    QString result_filename;
    QLabel statusLabel;

    // Is an image being shown
    bool showingImage;

    void setDefaults();
    void setRanges();
    int setupData();
    void freeImage();
    int getCountryIndex(QString countryName);
    bool eventFilter(QObject *obj, QEvent *event);
    void parseStations(vector<QString> &numbers, vector<QString> &rejected);
    int tidyUp();

protected:
    void resizeEvent ( QResizeEvent * event );
};

#endif // TEMPGRAPH_H
